#import "Deck.h"

@implementation Deck
+ (Deck*) getSuffledDeck {
    Deck *d = [[Deck alloc] init];
    
    for(int j = 1; j <= 4; j++) {
        for(int i = 1; i <= 13;i++) {
            Card *c = [[Card alloc] initWithType: j AndValue: i];
            if(c) [d->cards addObject: c];
        }
    }

    return [Deck shuffle: d];
}

+ (Deck*) shuffle: (Deck*) deckToBeShuffled {
    srandom(time(NULL));
    NSUInteger count = [deckToBeShuffled->cards count];
    for (NSUInteger i = 0; i < count; ++i) {
        int elements = count - i;
        int n = (random() % elements) + i;
        [deckToBeShuffled->cards exchangeObjectAtIndex:i withObjectAtIndex:n];
    }    
    
    return deckToBeShuffled;
}

- (id) init {
    self = [super init];
    self->cards = [[NSMutableArray alloc] init];
    return self;
}

- (Deck*) initWithDeck: (Deck*) allocatedDeck {
    if(allocatedDeck) self = allocatedDeck;
    return self;
}

- (Card*) getCardAtIndex: (int) index {
    return [cards objectAtIndex: index];
}

- (void) dealloc {
    [cards release];
    [super dealloc];
}

@end
